<?php

class OrdersController extends Controller {

    public $layout = 'main';

    /**
     * @var返回过滤数组
     * @author nirsh
     * @version 5.0
     * @demo 
     */
    public function filters() {
        return array(
            'accessControl', // perform access control for CRUD operations
        );
    }

    /**
     * @var返回验证条件 如果不需要验证权限 把$isPriv 设置为allow
     * @author nirsh
     * @version 5.0
     * @demo 
     *    switch($action){
     *      case "login":
     *          $isPriv = 'allow'
     *          break;
     *      default:
     *          $isPriv = $this->checkAccess($controller, $action);
     *      }
     */
    public function accessRules() {
        $controller = Yii::app()->getController()->id;
        $action = Yii::app()->getController()->action->id;
        //登录不需要判断权限
        $isPriv = $this->checkAccess($controller, $action);
        return array(
            array($isPriv,
                'actions' => array($action),
                'users' => array('*'),
            )
        );
    }

    /**
     * 访问某条具体的记录.
     * @param $id 某条记录的主键
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionView($id) {
        $model = $this->loadModel($id);
        //产品
        $items = OrdersItems::model()->findAllByAttributes(array('order_number' => $model->order_number));
        //客户信息
        $customer = OrdersCustomer::model()->findByAttributes(array('order_number' => $model->order_number));
        //支付信息
        $payment = OrdersPaypal::model()->findByAttributes(array('order_number' => $model->order_number));
        //处理日志
        $logModel = new OrdersLog();
        $logs = $logModel->findAllByAttributes(array('order_number' => $model->order_number));
        $this->render('view', array(
            'model' => $model,
            'items' => $items,
            'customer' => $customer,
            'payment' => $payment,
            'logModel' => $logModel,
            'logs' => $logs,
        ));
    }

    /**
     * 创建动作
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionCreate() {
        $model = new Orders;

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if (isset($_POST['Orders'])) {
            $model->attributes = $_POST['Orders'];
            if ($model->save()) {
                $url = $this->rewriteCreateUrl('view', array('id' => $model->id));
                $this->redirect($url);
            }
        }

        $this->render('create', array(
            'model' => $model,
        ));
    }

    /**
     * 更新数据 
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionUpdate($id) {
        $model = $this->loadModel($id);

        // Uncomment the following line if AJAX validation is needed
        // $this->performAjaxValidation($model);

        if (isset($_POST['Orders'])) {
            $model->attributes = $_POST['Orders'];
            if ($model->save()) {
                $url = $this->rewriteCreateUrl('view', array('id' => $model->id));
                $this->redirect($url);
            }
        }

        $this->render('update', array(
            'model' => $model,
        ));
    }

    /**
     * 删除记录 
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionDelete() {
        $sId = $_REQUEST['id'];
        if (is_array($sId)) {
            foreach ($sId as $id) {
                $tModel = $this->loadModel($id);
                $tModel->delete();
            }
        } else {
            $tModel = $this->loadModel($sId);
            $tModel->delete();
        }
        if (isset($_REQUEST['ajax'])) {
            echo "菜单删除成功";
        } else {
            $this->redirect($this->rewriteCreateUrl('admin'));
        }
    }


    /**
     * 数据管理
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function actionAdmin() {
        $model = new Orders('search');
        $model->unsetAttributes();  // clear any default values
        if (isset($_REQUEST['Orders']))
            $model->attributes = $_REQUEST['Orders'];
        if($_REQUEST['pay_mail']){
            $data = OrdersPaypal::model()->findAll("payer_email='".$_REQUEST['pay_mail']."'");
            $model->pay_oreder_number = $data;
        }
        $this->render('admin', array(
            'model' => $model,
        ));
    }
    
    public function actionApi(){
        $order_number = $_POST['order_number'];
        $status = $_POST['status'];
        $comment = $_POST['comment'];
        $model = Orders::model()->findByAttributes(array('order_number' => $order_number));
        //写入订单处理日志
        $logModel = new OrdersLog;
        $logModel->setIsNewRecord(TRUE);
        $logModel->unsetAttributes();
        $logModel->attributes = array(
            'order_number' => $order_number,
            'status' => $status,
            'comment' => $comment,
            'uid' => Yii::app()->request->cookies['uid']->value,
        );
        if(!$logModel->save())
        {
            print_r($logModel->errors);
        }
        
        echo Orders::model()->updateByPk($model->id, array('status' => $status));
    }

    /**
     * 返回数据模型是基于主键所得到的变量 
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    public function loadModel($id) {
        $model = Orders::model()->findByPk((int) $id);
        if ($model === null)
            throw new CHttpException(404, 'The requested page does not exist.');
        return $model;
    }

    /**
     * ajax验证
     * @param 主键id
     * @author nirsh
     * @demo 
     * @modifly 
     * @version 5.0
     */
    protected function performAjaxValidation($model) {
        if (isset($_POST['ajax']) && $_POST['ajax'] === 'orders-form') {
            echo CActiveForm::validate($model);
            Yii::app()->end();
        }
    }

}
